<template>
  <div class="page-container page-footer-btn">
    <van-cell-group>
      <FortenScan
        ref="scan"
        propPlaceholder="请扫描RFID/SN"
        propErrorMsg="请扫描正确的RFID/SN"
        :propTypeArr="['sn','rfid']"
        @scanCall="handleScanOver"
      />

      <van-divider><span style="font-size:15px">库存信息</span></van-divider>
      <van-field label="耗材名称" :value="form.matrName" readonly />
      <van-field label="耗材品规" :value="form.matrSpec" readonly />
      <van-field label="供应商" :value="form.supplierName" readonly />
      <van-field label="批次" :value="form.lot" readonly />
      <van-field label="生产日期" :value="form.mfgDate" readonly />
      <van-field label="有效期" :value="form.expDate" readonly />
      <van-field label="包装单位" :value="form.packageUnit" readonly />
      <van-field label="包装单位数量" :value="form.packageQty" readonly />
      <van-field label="厂商" :value="form.mfgName" readonly />
      <van-field label="SN" :value="form.sn" readonly />
      <van-field label="RFID" :value="form.rfid" readonly />

      <van-row type="flex" justify="space-around" class="footer-btn"  gutter="12"  >
        <van-col span="8">
          <van-button type="primary" block  @click="handleUnBind" :disabled="strings.isBlank(form.rfid)">解除绑定</van-button>
        </van-col>
        <van-col span="8" >
          <van-button type="info" block  @click="handlePrint" :disabled="strings.isBlank(form.sn)">打印标签</van-button>
        </van-col>
        <van-col span="6" >
          <van-button type="default" block  @click="handleReSet">重置</van-button>
        </van-col>
    </van-row>
    </van-cell-group>
  </div>
</template>
<script>
import barcodeParse from '@/util/barcodeParse.js'

export default {
  data() {
    return {
      scan: '',
      form: {}
    }
  },
  props: {
    propFlowKey: String
  },
  components: {
  },
  methods: {
    //扫描
    async handleScanOver(scanValue) {
      let codeType = barcodeParse.barCodeType(scanValue)
      if (codeType != 'SN' && codeType != 'RFID') {
        this.$notify.warning('请扫描有效的SN或RFID')
        return
      }

      if (codeType == 'RFID' && this.form.sn && !this.form.rfid) { //RFID绑定
        //先检查rfid是否被使用
        let wrapper = this.$newWrapper().eq('rfid', scanValue)
        this.$ajax.get('/wm/stock/list', {
          params: {
            isScan: 1,
            queryString: JSON.stringify(wrapper.queryConditions)
          }
        }).then(resp => {
          if (resp.data && resp.data.size > 0) {
            this.$notify.warning('RFID已被使用，不能重复绑定')
            return
          }
          this.$ajax.get(`wm/stock/rfidBindOrUnbind?opType=bind&sn=${this.form.sn}&rfid=${scanValue}&isScan=1`).then(resp => {
            this.$notify.success(resp.msg)
            this.$set(this.form, 'rfid', this.scan)
            // this.form = []
            this.scan = ''
            this.$refs.scan.focus()
          })
        })
        return
      }
      let params = {
        isScan: 1,
        uniqueCode: scanValue
      }
      this.$ajax.get('/wm/stock/getStockByUniqueCode', {params}).then(resp => {
        let currentStock = resp.data
        if (!currentStock) {
          this.$notify.warning('不能识别的SN或RFID！')
          return
        }
        this.form = currentStock
        this.scan = ''
        this.$refs.scan.focus()
      })
    },
    handleReSet() {
      this.form = {}
    },
    handlePrint() {
      // const params = {
      //   labelKey: 'sn',
      //   data: this.form
      // }
      // this.$ajax.post('/sys/printer/printLabel', params).then(res => {
      //   this.$notify.success('已推送打印标签')
      // })
      this.utilPrint.print('other', -1, [this.form.sn])
    },
    handleUnBind() {
      this.$dialog.confirm({
        title: '标题',
        message: '是否解绑该RFID与SN绑定关系?',
      }).then(() => {
        this.$ajax.get(`wm/stock/rfidBindOrUnbind?opType=unbind&sn=${this.form.sn}&rfid=null`).then((resp) => {
          this.$notify.success(resp.msg)
          this.form.rfid = ''
          this.scan = ''
          this.$refs.scan.focus()
        })
      }).catch(() => {
        // on cancel
      })
    }
  },
  mounted() {
    this.$refs.scan.focus()
  }
}
</script>

<style lang="scss" scoped>
  .van-col {
    text-align: center;
  }
</style>
